﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Zhongy
{
    public class GameHandler : IHandler
    {
        private UserCache userCache = Cache.User;


        public override void OnDisconnect(ClientPer client)
        {

        }

        public override void OnReceive(ClientPer client, SocketReceiveMsg msg)
        {
                
            switch (msg.MT)
            {
                case OpCode.TYPE_START_GAME_C:
                    if (msg.Restore == 1)
                    {
                        ClientResumeGame(client, msg);
                    }
                    else
                    {
                        ClientStartGame(client, msg);
                    }
                    break;

                case OpCode.TYPE_STOP_GAME_C:
                    if (msg.Pause == 1)
                    {
                        ClientPauseGame(client, msg);
                    }
                    else
                    {
                        ClientStopGame(client, msg);
                    }
                    break;

                default:
                    Debug.LogError("信息归属不对");
                    break;
            }
        }


        private void ClientStartGame(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (msg.Player == null || msg.Player.Count <= 0)
                {
                    Debug.LogError("游戏开始命令数据有误");
                }
                foreach (string id in msg.Player)
                {
                    if (!userCache.IsExsitUser(id))
                        Debug.LogError(string.Format("{0}该用户并未加入游戏用户列表", id));
                }

                if (msg.Option == null || msg.Option.Time < 0 || msg.Option.Difficulty < -1)
                {
                    Debug.LogError(string.Format("游戏设置数据无效"));
                }

                Debug.Log(string.Format("client发来游戏时长为{0}", msg.Option.Time));
                Debug.Log(string.Format("client发来游戏难度为{0}", msg.Option.Difficulty));

                if (msg.Option == null || msg.Option.Time <= 0)
                {
                    Debug.LogError("设置为空或者设置时间无效");
#if DEBUG_MODEL
                    msg.Option.Time = 20;
                    msg.Option.Difficulty = 0;
#endif
                    Debug.LogError("debug设置为20秒");

                }

                SocketSendMsg sendMsg = new SocketSendMsg
                {
                    MT = OpCode.TYPE_ACK2C,
                    ackid = msg.mesgid,
                    result = "OK",
                    mesg = string.Empty
                };

                GameConfig.CurGameTime = msg.Option.Time;
                GameConfig.CurGameDifficulty = msg.Option.Difficulty;

                Dispatch(TransCode.Net, NetEvtCode.Client2TcpMsg, new object[] {client, sendMsg});

                //start game
                Dispatch(TransCode.Net, GameEvtCode.GameStart);
                Dispatch(TransCode.Game, GameEvtCode.GameStart, new object[]{msg.Option.Time,msg.Option.Difficulty});
            });
        }

        private void ClientStopGame(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                SocketSendMsg sendMsg = new SocketSendMsg
                {
                    MT = OpCode.TYPE_ACK2C,
                    ackid = msg.mesgid,
                    result = "OK",
                    mesg = string.Empty
                };

                Dispatch(TransCode.Net, NetEvtCode.Client2TcpMsg, new object[] { client, sendMsg });

                //todo stopgame
                Dispatch(TransCode.Net, GameEvtCode.GameStop);
                Dispatch(TransCode.Game,GameEvtCode.GameStop);
            });
        }

        private void ClientPauseGame(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                SocketSendMsg sendMsg = new SocketSendMsg
                {
                    MT = OpCode.TYPE_ACK2C,
                    ackid = msg.mesgid,
                    result = "OK",
                    mesg = string.Empty
                };

                Dispatch(TransCode.Net,NetEvtCode.Client2TcpMsg,new object[]{client,sendMsg});
                //todo pausegame
                Dispatch(TransCode.Net, GameEvtCode.GamePuase);
                Dispatch(TransCode.Game, GameEvtCode.GamePuase);

            });
        }

        private void ClientResumeGame(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (msg.Player == null || msg.Player.Count <= 0)
                {
                    Debug.LogError("游戏开始命令数据有误");
                }

                foreach (string id in msg.Player)
                {
                    if (!userCache.IsExsitUser(id))
                        Debug.LogError(string.Format("{0}该用户并未加入游戏用户列表", id));
                }

                Debug.LogError("继续开始");
                Debug.LogError("时长：" + msg.Option.Time);
                Debug.LogError("难度：" + msg.Option.Difficulty);

                SocketSendMsg sendMsg = new SocketSendMsg
                {
                    MT = OpCode.TYPE_ACK2C,
                    ackid = msg.mesgid,
                    result = "OK",
                    mesg = string.Empty
                };
                Debug.LogError("ClientResumeGame");
                //client.Send(sendMsg);
                Dispatch(TransCode.Net, NetEvtCode.Client2TcpMsg, new object[] {client, sendMsg});

                //todo resumegame
                Dispatch(TransCode.Net, GameEvtCode.GameStart, new object[] {"resume"});
                Dispatch(TransCode.Game, GameEvtCode.GameStart, new object[] {"resume"});
            });
        }

    }
}
